package com.zjz.springcloud.controller;



import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zjz.springcloud.pojo.Dept;
import com.zjz.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// 提供Restful服务
@RestController
public class DeptController {


    @Autowired
    private DeptService deptService;


    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/dept/get/{depton}")
    public Dept get(@PathVariable("depton") Long depton){
        Dept dept = deptService.queryById(depton);
        if(dept==null){
            throw new RuntimeException("这个depton:"+depton+"不存在");
        }
        return dept;
    }


    // 备选方案

    public Dept hystrixGet(@PathVariable("depton")Long depton){

        return new Dept()
                .setDeptno(depton)
                .setDname("这个depton:"+depton+"不存在,null--@Hystrix")
                .setDb_source("no this database in mysql");
    }





}
